package edu.sdjzu.exam.mapper.exam;

import edu.sdjzu.exam.domain.exam.ExamAssignment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ExamAssignmentMapper {
    @Insert("INSERT INTO Exam_Assignments (exam_id, group_id, start_time, end_time, created_by) " +
            "VALUES (#{examId}, #{groupId}, #{startTime}, #{endTime}, #{createdBy})")
    @Options(useGeneratedKeys = true, keyProperty = "assignmentId")
    void insertExamAssignment(ExamAssignment examAssignment);

    @Select("SELECT * FROM Exam_Assignments WHERE assignment_id = #{assignmentId}")
    ExamAssignment selectExamAssignmentById(Long assignmentId);

    @Update("UPDATE Exam_Assignments SET exam_id = #{examId}, group_id = #{groupId}, " +
            "start_time = #{startTime}, end_time = #{endTime} WHERE assignment_id = #{assignmentId}")
    void updateExamAssignment(ExamAssignment examAssignment);

    @Delete("DELETE FROM Exam_Assignments WHERE assignment_id = #{assignmentId}")
    void deleteExamAssignment(Long assignmentId);

    @Select("SELECT * FROM Exam_Assignments WHERE created_by = #{createdBy}")
    List<ExamAssignment> selectExamAssignmentsByCreator(Long createdBy);


    @Select("SELECT * FROM Exam_Assignments WHERE group_id = #{groupId}")
    List<ExamAssignment> selectExamAssignmentsByGroupId(Long groupId);

    @Select("SELECT ea.* FROM Exam_Assignments ea " +
            "JOIN User_Group_Members ugm ON ea.group_id = ugm.group_id " +
            "WHERE ugm.user_id = #{studentId}")
    List<ExamAssignment> selectExamAssignmentsByStudent(Long studentId);
}
